package com.example.secondhandsysapi.Controller;

import com.example.secondhandsysapi.Entity.OrderInfo;
import com.example.secondhandsysapi.Service.OrderInfoService;
import com.example.secondhandsysapi.Vo.AwaitingShipment;
import com.example.secondhandsysapi.Vo.Money;
import com.example.secondhandsysapi.Vo.NewUserInfo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("order")
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;

    // http://localhost:8080/order/querybydate
    @GetMapping("/querybydate")
    public List<OrderInfo> getOrderInfoByDate(@RequestParam String date) {
        return orderInfoService.getOrderInfoByDate(date);
    }

    // http://localhost:8080/order/querybystate
    @GetMapping("/querybystate")
    public List<OrderInfo> getOrderInfoByState(@RequestParam String state) {
        return orderInfoService.getOrderInfoByState(state);
    }

    // http://localhost:8080/order/queryorder  get order
    @GetMapping("/queryorder")
    public List<OrderInfo> queryOrderInfoByDateAndState(@RequestParam(required = false)  String date,@RequestParam(required = false)  String state) {
        List<OrderInfo> orderallinfo = null;
        if(date != null && state != null){
            // 当日期和状态都不为空时，根据日期和状态查订单信息
            orderallinfo = orderInfoService.getOrderInfoByDateAndState(date, state);
        } else if(date != null) {
            // 当日期不为空时，根据日期查订单信息
            orderallinfo = orderInfoService.getOrderInfoByDate(date);
        } else if(state != null) {
            // 当状态不为空时，根据状态查订单信息
            orderallinfo = orderInfoService.getOrderInfoByState(state);
        } else {
            // 当日期和状态都为空时，返回所有订单信息
            orderallinfo = orderInfoService.getOrderInfo();
        }
        return orderallinfo;
    }


    @GetMapping("/page")
    public PageInfo<OrderInfo> queryOrderInfoByPage(@RequestParam Integer page,@RequestParam Integer size){
        return orderInfoService.getOrderPageInfo(page,size);
    }

    // http://localhost:8080/order/AwaitingShipment
    @GetMapping("/AwaitingShipment")
    public List<AwaitingShipment> AwaitingShipment(){
        return orderInfoService.getAwaitingShipmentOrderInfoByState();
    }

    // http://localhost:8080/order/AwaitingPayment
    @GetMapping("/AwaitingPayment")
    public List<AwaitingShipment> AwaitingPayment(){
        return orderInfoService.getAwaitingPaymentOrderInfoByState();
    }


    // http://localhost:8080/order/RoadingShipment  在途中
    @GetMapping("/RoadingShipment")
    public List<AwaitingShipment> RoadingShipment(){
        return orderInfoService.getRoadingShipmentOrderInfoByState();
    }

    // http://localhost:8080/order/FinishingShipment  已完成
    @GetMapping("/FinishingShipment")
    public List<AwaitingShipment> FinishingShipment(){
        return orderInfoService.getFinishingShipmentOrderInfoByState();
    }

// http://localhost:8080/order/AllOrders
    @GetMapping("/AllOrders")
    public List<AwaitingShipment> AllOrders(){
        return orderInfoService.getAllOrderInfoByState();
    }

    // http://localhost:8080/order/UpdateAddress
    @GetMapping("/UpdateAddress")
    public Integer UpdateAddress(@RequestParam String dilivery_address,@RequestParam Integer order_id){
       Integer i =orderInfoService.updateDeliveryAddress(dilivery_address,order_id);
       return i;
    }

    // http://localhost:8080/order/UpdateState
    @GetMapping("/UpdateState")
    public Integer UpdateState(@RequestParam String order_state,@RequestParam Integer order_id){
         return orderInfoService.updateOrderState(order_state,order_id);
    }

    @GetMapping("del/{order_id}")
    public Integer delOrder(@PathVariable Integer order_id){
        Integer i =orderInfoService.deleteOrderInfo(order_id);
        return i;
    }

    //获取对应日期的总交易额
    // http:localhost:8080/order/MoneyByDay
    @PostMapping("MoneyByDay")
    public Money MoneyByDay(@RequestBody Money money){
        Money result = new Money();
        List<Double> total_price = new ArrayList<>();
        List<Date> orderfinishtimes = new ArrayList<>();

        for (Date date : money.getorder_finishtime()) {
            Double count = 0.0;
            count = orderInfoService.getMoneyByDay(date);
            total_price.add(count);
            orderfinishtimes.add(date);
        }
        result.settotal_price(total_price);
        result.setorder_finishtime(orderfinishtimes);
        return result;
    }

}

